//
//  HYShowLoading.m
//  Booking
//
//  Created by Duntech on 2018/5/11.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYShowLoading.h"

static NSInteger LoadingImgViewWidth = 80;
static NSInteger loadingImgViewHeight = 80;

@interface HYShowLoading ()

@property (nonatomic,strong) UIImageView *loadingImageView;

@end

@implementation HYShowLoading

+ (instancetype)shareInstance{
    
    static HYShowLoading *showLoading;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        showLoading = [[HYShowLoading alloc] init];
    });
    return showLoading;
}

#pragma mark - lifeCycle
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        //you can custom your loading style
        [self addSubview:self.loadingImageView];
    }
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    self.loadingImageView.frame = CGRectMake(0, 0, LoadingImgViewWidth, loadingImgViewHeight);
    self.loadingImageView.center = self.center;
}

#pragma mark - public method
+ (void)showLoadingOnView:(UIView *)view{
    
    [self showLoadingOnView:view allowUserInteraction:NO];
}

+ (void)showLoadingOnView:(UIView *)view allowUserInteraction:(BOOL)allowUserInteraction{
    
    HYShowLoading *loading = [[self shareInstance] initOnView:view isShowHUD:NO];
    [loading showViewWithAnimate:YES];
    loading.userInteractionEnabled = allowUserInteraction;
}

+ (void)hideLoadingOnView:(UIView *)view{
    
    [[self shareInstance] hideAnimated:YES];
}

#pragma mark - private method
- (instancetype)initOnView:(UIView *)view isShowHUD:(BOOL)isShowHUD{
    
    return [self initWithFrame:view.bounds];
}

- (void)showViewWithAnimate:(BOOL)animate{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hideViewWithAnimate:(BOOL)animate{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - lazyload
- (UIImageView *)loadingImageView{
    
    if (!_loadingImageView) {
       
        _loadingImageView = [UIImageView new];
        _loadingImageView.contentMode = UIViewContentModeScaleAspectFill;
        _loadingImageView.clipsToBounds = YES;
    }
    return _loadingImageView;
}

@end
